import json
import time

import requests

def getAccessToken():
    url="https://digitalvolvo.com/openApi/accessToken?wsId=tAeLHd46LCLbwA7N9zytWw==&wsSecret=b635f210-09e5-4f3d-95f9-346fa83f27b4"
    response = requests.get(url)
    if response.status_code == 200:
        return response.json().get('result').get("accessToken")



def getAuditLog(accessToken,applyId):
    # 定义API地址和参数
    url = "https://digitalvolvo.com/openApi/apply/"+applyId+"/auditRecord"

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
        'accessToken': accessToken
    }

    try:
        # 发送GET或POST请求并获取返回结果
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            result = response.json()
            # 处理返回结果
            if (result.get("errCode") == 49300):
                print("无效的token")
            if (result.get("errCode") == 40023):
                print("该申请不存在")
            return result
        else:
            print("Request failed with status code", response.status_code)
    except Exception as e:
        print("An error occurred:", str(e))


def audit(accessToken,applyId,userId,auditNodeId):
    url="https://digitalvolvo.com/openApi/"+applyId+"/audit"

    headers = {
        'userId':userId,
        'accessToken': accessToken
    }
    bodys = {
        "auditResult":2,##1 [int] 提交处理 2 [int] 通过 3 [int] 拒绝 5 [int] 发起申请/申请人草稿提交 6 [int] 申请人完善申请并提交 7 [int] 抄送
        "auditNodeId":auditNodeId, ##执行处理的处理节点id，可通过调用【获取单条数据的流程日志】接口获取
        "auditFeedback":""##处理反馈内容
    }
    print("==========>audit参数=============")
    print(headers)
    print(bodys)
    print("==========>audit结果=============")
    response = requests.post(url, headers=headers, data=json.dumps(bodys))
    print(response.json())

##https://help.qingflow.com/help-docs?type=qingCode&docId=0122a5876425b78206cbe436426dca23&parentId=

dev_audit_user = ["JLIN50","ZYAO13","ZLIU217","BLI107","XYANG137","HCHEN142","FJIAO2","YZHAN485","BZHANG97"]

while 1==1:
    time.sleep(2)
    applyId = "48241113"
    accessToken = getAccessToken()
    print("accessToken")
    print(accessToken)
    log = getAuditLog(accessToken,applyId)
    print("===log")
    print(log)
    applyStatus = log.get("result").get("applyStatus")
    if(applyStatus == 3 or applyStatus == 4):
        print("=================审批通过======")
        break
    auditNodeId = log.get("result").get("currentNodes")[0].get("auditNodeId")
    print(auditNodeId)
    userId = log.get("result").get("currentNodes")[0].get("waitAuditUserList")[0].get("userId")
    print("审核人员:"+userId)
    if userId in dev_audit_user:
        print("审核人员在开发人员组里面 开始审核操作")
        audit(accessToken,applyId,userId,auditNodeId)
    else :
        print("审核人员非开发人员 请检查")